package com.ujson.uaa.service.impl;

import com.ujson.base.common.dto.Res;
import com.ujson.uaa.repository.UserRepository;
import com.ujson.uaa.service.UserService;
import com.ujson.uaa.types.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

@Service
public class CustomUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private UserRepository userRepository;
    private UserMapper userMapper = UserMapper.INSTANCE;

    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        com.ujson.uaa.types.User user = userRepository.findByUserName(username);
        if (Objects.isNull(user)) {
            throw new UsernameNotFoundException("username: " + username + " not found.");
        }
        return User.withUsername(username).password(user.getPassword()).authorities("admin").build();
//        return User.withUsername(username).password("{noop}" + userMap.get(username)).authorities("admin").build();
    }
}
